<?php

	/**
	 * Crawls lyrics item from elyrics.net
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class LyricsLog implements LyricsSource
	{
		/* VARIABLES */
		
		/**
		 * Lyrics id
		 *
		 * @var int
		 */
//		public $id;
		
		/**
		 * Lyrics list base URL
		 *
		 * @var string
		 */
		public $listUrl = 'http://www.lyricslog.net/lyrics';
		
		/**
		 * Lyrics item base URL
		 *
		 * @var string
		 */
		public $itemUrl = 'http://www.lyricslog.net/lyrics/lyrics/';
		
		/**
		 * Lyrics URLs
		 *
		 * @var array
		 */
		public $lyricsURLs;
		
		
		/* CONSTRUCTORS */
			
		/**
		 * Defualt constructor
		 *
		 */
		public function LyricsLog()
		{
//			$this->id = $id;
		}		
		
		/* METHODS */
		
		/**
		 * Gets list of lyrics URLs
		 *
		 * @param int $fromPageNumber from page
		 * @param int $toPageNumber to page
		 * 
		 */
		public function GetLyricsUrls($fromPageNumber, $toPageNumber)
		{
			$lyrics = array();
			
			for ($i = $fromPageNumber; $i <= $toPageNumber; $i++)
			{
				//parsed page
				$page = phpQuery::newDocumentFileHTML($this->listUrl."?page=".$i);
				
				$results = $page->find(".search_result > a:first-child");
				foreach ($results as $result)
				{
					$this->lyricsURLs[] = pq($result)->attr('href');
				}
				
			}
		}
		
		/**
		 * Saves lyrics URLs
		 * 
		 * @return boolean
		 *
		 */
		public function SaveLyricsURLs()
		{
			$result = array('success' => 0, 'fail' => 0);
			
			foreach ($this->lyricsURLs as $url)
			{
				$lyricsItem = new LyricsItem();
				$saveResult = $lyricsItem->SaveURL($url, 'lyricslog');
				if ($saveResult)
				{
					$result['success']++;
				}
				else 
				{
					$result['fail']++;
				}
			}
			
			return $result;
		}
		
		/**
		 * Gets lyrics URLs list
		 *
		 * @param int $fromRecord offset
		 * @param int $toRecord count
		 */
		public function GetSavedLyricsURLs($fromRecord = '', $toRecord = '')
		{
			//limit
			if (($fromRecord == '') && ($toRecord == ''))
			{
				$limit = '';
			}
			else 
			{
				$fromRecord = ($fromRecord == '') ? 0 : $fromRecord;
				$toRecord = ($toRecord == '') ? 1000000 : $toRecord;
				$limit = "LIMIT $fromRecord, $toRecord";
			}
			
			$urls = DB::ListItems("
				SELECT * FROM t_lyrics_urls WHERE source = 'lyricslog' $limit;
			");
			
			return $urls;
		}
		
		/**
		 * Gets lyrics item
		 * 
		 * @param int $id lyrics id
		 * 
		 * @return LyricsItem
		 *
		 */
		public function GetLyricsItem($url)
		{
			//parsed page
//			$ch = curl_init();
//			curl_setopt($ch, CURLOPT_URL, $url);
//			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//			$contents = curl_exec($ch);
//			curl_close ($ch);			
//			$page = phpQuery::newDocumentHTML($contents);
			$page = phpQuery::newDocumentFileHTML($url);
			
			//lyrics block
			$lyricsBlock = $page->find("#left2_panel");

			//lyrics info
			$lyricsInfo = pq($lyricsBlock)->find("#lyrics_info > ul");
			
			//lyrics item
			$lyricsItem = new LyricsItem();
			
			//id
			$urlElements = explode('/', $url);
			$lyricsItem->id = $urlElements[count($urlElements) - 2];
			
			//display name
			$lyricsItem->displayName = trim(pq($lyricsBlock)->find("#heading_tan .fn")->html());
			
			//year
			$lyricsItem->vintage = intval(substr($lyricsItem->displayName, -4));
		
			//name
			if (empty($lyricsItem->vintage))
			{
				$lyricsItem->vintage = 'NULL';
				$lyricsItem->name = $lyricsItem->displayName;
			}
			else 
			{
				$lyricsItem->name = trim(str_replace($lyricsItem->vintage, '', $lyricsItem->displayName));
			}	
			
			//lyrics info
			$lyricsInfoElements = array('region', 'lyricsry', 'type', 'variety', '', '', '', '');
			foreach ($lyricsInfo->children() as $key => $infoItem)
			{
				switch ($lyricsInfoElements[$key])
				{
					//TODO: region/subregion division
					case 'region':
						$lyricsItem->region = pq($infoItem)->find("a")->html();
						$lyricsItem->subRegion = 'NULL';
						break;
						
					case 'lyricsry':
						$lyricsItem->producer = pq($infoItem)->find("a")->html();
						break;
						
					case 'type':
						$lyricsItem->type = pq($infoItem)->find("a")->html();
						break;
						
					case 'variety':
						$lyricsItem->varietal = pq($infoItem)->find("a")->html();
						break;
						
				}
			}
			
			//item url
			$lyricsItem->webAddress = $url;
			
			//image url
			$lyricsItem->photoURL = pq($lyricsInfo)->find("#lyrics_pic img")->attr('src');
			
			//source
			$lyricsItem->source = 'lyricslog';
			
			return $lyricsItem;
		}
	}

?>